﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSqlServer;

/// <summary>
/// TEmployee 的摘要说明
/// </summary>
public class TEmployee : TData
{
    //常量
    const string CTableName = "t_employee";
    const string CKeyName = "employee_id";

    //与数据表字段对应的变量
    private string FEmployeeID;
    private string FName;
    private string FEnName;
    private string FNickName;
    private string FSex;
    private string FIs_Married;
    private string FTel;
    private string FAddr;
    private string FIMAccount;
    private string FIs_Sale;
    private string FDept;
    private DateTime FBirthday;
    private string FEMail;
    private string FTel2;
    private string FZip;
    private string FPic;
    private string FPWD;
    private string FIs_Use;
    private string FRoleID;
    private string FNotice;
    private string FInputer;
    private DateTime FInput_Time;
    private DateTime FModify_Time;

    //构造函数
    public TEmployee()
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
    }

    //带参数的构造函数
    public TEmployee(string EmployeeID)
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
        FKeyValue = EmployeeID;
    }

    public TEmployee(System.Web.UI.Page page)
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
        FKeyValue = (string)page.Session["uid"];
    }

    //根据EmployeeID获得记录值
    public override void Select()
    {
        string strSql = GetSelectString();
        TSqlDB oDB = new TSqlDB(strSql);
        if (!oDB.EOF)
        {
            FEmployeeID = oDB["employee_id"].ToString();
            FName = oDB["name"].ToString();
            FEnName = oDB["en_name"].ToString();
            FNickName = oDB["nickname"].ToString();
            FSex = oDB["sex"].ToString();
            FIs_Married = oDB["is_married"].ToString();
            FTel = oDB["tel"].ToString();
            FAddr = oDB["addr"].ToString();
            FIMAccount = oDB["im_account"].ToString();
            FIs_Sale = oDB["is_sale"].ToString();
            FDept = oDB["dept"].ToString();
            FBirthday = oDB["birthday"].ToDateTime();
            FEMail = oDB["email"].ToString();
            FTel2 = oDB["tel2"].ToString();
            FZip = oDB["zip"].ToString();
            FPic = oDB["pic"].ToString();
            FPWD = oDB["pwd"].ToString();
            FIs_Use = oDB["is_use"].ToString();
            FRoleID = oDB["role_id"].ToString();
            FNotice = oDB["notice"].ToString();
            FInputer = oDB["inputer"].ToString();
            FInput_Time = oDB["input_time"].ToDateTime();
            FModify_Time = oDB["modify_time"].ToDateTime();
        }
        oDB.Free();
    }

    //插入新记录。
    public void Insert()
    {
        string ExSql = "'" + FEmployeeID + "','" + FName + "','" + FEnName + "','" + FNickName + "','" + FSex + "','" + FIs_Married + "','" + FTel + "','" + FAddr + "',";
        ExSql = ExSql + "'" + FIMAccount + "','" + FIs_Sale + "','" + FDept + "','" + FBirthday.ToString() + "','" + FEMail + "','" + FTel2 + "','" + FZip + "',";
        ExSql = ExSql + "'" + FPic + "','" + FPWD + "','" + FIs_Use + "','" + FRoleID + "',";
        ExSql = ExSql + "'" + FNotice + "','" + FInputer + "','" + FInput_Time.ToString() + "','" + FModify_Time.ToString() + "'";
        base.Insert(ExSql);
    }

    //更新记录。
    public void Update()
    {
        string ExSql = "name='" + FName + "',en_name='" + FEnName + "',nickname='" + FNickName + "',sex='" + FSex + "',is_married='" + FIs_Married + "',tel='" + FTel + "',";
        ExSql = ExSql + "addr='" + FAddr + "',im_account='" + FIMAccount + "',is_sale='" + FIs_Sale + "',dept='" + FDept + "',birthday='" + FBirthday.ToString() + "',";
        ExSql = ExSql + "email='" + FEMail + "',tel2='" + FTel2 + "',zip='" + FZip + "',";
        ExSql = ExSql + "is_use='" + FIs_Use + "',notice='" + FNotice + "',modify_time='" + FModify_Time.ToString() + "'";
        base.Update(ExSql);
    }

    //更新记录。
    public void Update2()
    {
        string ExSql = "name='" + FName + "',en_name='" + FEnName + "',nickname='" + FNickName + "',tel='" + FTel + "',";
        ExSql = ExSql + "im_account='" + FIMAccount + "',dept='" + FDept + "',";
        ExSql = ExSql + "email='" + FEMail + "',tel2='" + FTel2 + "'";
        base.Update(ExSql);
    }

    //保存照片字段
    public void UpdatePic()
    {
        string ExSql = "pic='" + FPic + "'";
        base.Update(ExSql);
    }

    //静态，可以不用创建对象即可调用。
    public static void DeleteByID(string EmployeeID)
    {
        TData.DeleteByID(EmployeeID, CTableName, CKeyName);
    }

    //初始化密码为1234
    public void ResetPWD()
    {
        string ExSql = "pwd='" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("1234", "MD5") + "'";
        base.Update(ExSql);
    }

    //更改密码
    public void ChangePWD(string sPWD)
    {
        string ExSql = "pwd='" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sPWD, "MD5") + "'";
        base.Update(ExSql);
    }

    //更改员工状态
    public void ChgStatus()
    {
        TSqlDBProc oProc = new TSqlDBProc("ms_ChgStatus", 0);
        oProc.ms_ChgStatus(FKeyValue);
    }

    /// <summary>
    /// （静态）私有：根据用户名和密码检查用户身份
    /// 用户名和密码正确则返回UID，否则返回“”
    /// </summary>
    private static string CheckLoginReturnUID(string useruid, string userpwd)
    {
        useruid = useruid.Trim();
        userpwd = userpwd.Trim();

        string Result = "";
        bool isValid = false;

        // 验证数据库用户，并取UID
        string strSql = "select employee_id from t_employee where is_use='Y' and employee_id='" + useruid + "' and pwd='" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(userpwd, "MD5") + "'";
        TSqlDB oEmployee = new TSqlDB(strSql);
        isValid = !oEmployee.EOF;
        if (isValid)
            Result = oEmployee["employee_id"].ToString();
        oEmployee.Free();

        // 返回结果
        return Result;
    }

    /// <summary>
    /// （静态）根据用户名和密码检查用户身份
    /// 用户名和密码正确则返回“真”，否则返回“假”
    /// </summary>
    public static bool CheckLogin(string useruid, string userpwd)
    {
        bool Result = false;
        string UserID = CheckLoginReturnUID(useruid, userpwd);
        Result = UserID.Length > 0;

        // 返回结果
        return Result;
    }

    public static string GetCityCode(string uorg)
    {
        string result = "BJ";

        switch (uorg)
        {
            case "B01":
                result = "BJ";
                break;
            case "B02":
                result = "BJ";
                break;
            case "B03":
                result = "SH";
                break;
            case "B04":
                result = "GZ";
                break;
            case "B05":
                result = "HZ";
                break;
            case "B06":
                result = "SZ";
                break;
            default:
                result = "BJ";
                break;
        }

        return result;
    }

    /// <summary>
    /// （静态）根据用户名和密码检查用户身份
    /// 用户名和密码正确则返回“真”，否则返回“假”，并将UID保存在Session中
    /// </summary>
    public static bool CheckLogin(string useruid, string userpwd, System.Web.UI.Page page)
    {
        bool Result = false;
        string UserID = CheckLoginReturnUID(useruid, userpwd);
        Result = UserID.Length > 0;

        // 保存Session
        if (Result)
        {
            string sSql = "select name,left(dept,3),dept from t_employee where employee_id='" + UserID + "'";
            TSqlDBReader oReader = new TSqlDBReader(sSql);
            oReader.FDataReader.Read();
            page.Session["uname"] = oReader.FDataReader.GetValue(0).ToString();
            page.Session["uorg"] = oReader.FDataReader.GetValue(1).ToString();
            page.Session["citycode"] = GetCityCode(oReader.FDataReader.GetValue(1).ToString());
            page.Session["udept"] = oReader.FDataReader.GetValue(2).ToString();
            page.Session["uid"] = UserID;
            oReader.Free();
        }
        // 返回结果
        return Result;
    }

    /// <summary>
    ///（静态）检查用户是否是合法用户
    /// </summary>
    public static void CheckUser(System.Web.UI.Page page)
    {
        bool Result = false;

        TEmployee oEmployee = new TEmployee(page);
        Result = !oEmployee.isNull;

        if (!Result)
            //page.Server.Transfer("\\EasyAD_Sales\\golf.aspx");
            page.Server.Transfer("\\EasyCRM\\employee\\employee_login.aspx");
    }

    /// <summary>
    ///（静态）检查用户是否是管理员
    /// </summary>
    public static void CheckAdmin(System.Web.UI.Page page)
    {
        bool Result = false;

        if ((page.Session["uid"] != null) && (page.Session["uid"].ToString().Length > 0))
        {
            // 验证数据库用户
            string strSql = "select employee_id from t_employee where is_use='Y' and employee_id='" + (string)page.Session["uid"] + "' and name='管理员'";
            TSqlDB oEmployee = new TSqlDB(strSql);
            Result = !oEmployee.EOF;
            oEmployee.Free();
        }

        if (!Result)
            page.Server.Transfer("\\EasyCRM\\employee\\employee_login.aspx");
    }

    public bool isNull	// 各项属性是否为空
    {
        get
        {
            return (FKeyValue == null) || (FKeyValue == "");
        }
    }

    public string EmployeeID
    {
        get
        {
            return FEmployeeID;
        }
        set
        {
            FEmployeeID = value;
        }
    }

    public string Name
    {
        get
        {
            return FName;
        }
        set
        {
            FName = value;
        }
    }

    public string EnName
    {
        get
        {
            return FEnName;
        }
        set
        {
            FEnName = value;
        }
    }

    public string NickName
    {
        get
        {
            return FNickName;
        }
        set
        {
            FNickName = value;
        }
    }

    public string Sex
    {
        get
        {
            return FSex;
        }
        set
        {
            FSex = value;
        }
    }

    public string Is_Married
    {
        get
        {
            return FIs_Married;
        }
        set
        {
            FIs_Married = value;
        }
    }

    public string Tel
    {
        get
        {
            return FTel;
        }
        set
        {
            FTel = value;
        }
    }

    public string Addr
    {
        get
        {
            return FAddr;
        }
        set
        {
            FAddr = value;
        }
    }

    public string IMAccount
    {
        get
        {
            return FIMAccount;
        }
        set
        {
            FIMAccount = value;
        }
    }

    public string Is_Sale
    {
        get
        {
            return FIs_Sale;
        }
        set
        {
            FIs_Sale = value;
        }
    }

    public string Dept
    {
        get
        {
            return FDept;
        }
        set
        {
            FDept = value;
        }
    }

    public DateTime Birthday
    {
        get
        {
            return FBirthday;
        }
        set
        {
            FBirthday = value;
        }
    }

    public string EMail
    {
        get
        {
            return FEMail;
        }
        set
        {
            FEMail = value;
        }
    }

    public string Tel2
    {
        get
        {
            return FTel2;
        }
        set
        {
            FTel2 = value;
        }
    }

    public string Zip
    {
        get
        {
            return FZip;
        }
        set
        {
            FZip = value;
        }
    }

    public string Pic
    {
        get
        {
            return FPic;
        }
        set
        {
            FPic = value;
        }
    }

    public string PWD
    {
        get
        {
            return FPWD;
        }
        set
        {
            FPWD = value;
        }
    }

    public string Is_Use
    {
        get
        {
            return FIs_Use;
        }
        set
        {
            FIs_Use = value;
        }
    }

    public string RoleID
    {
        get
        {
            return FRoleID;
        }
        set
        {
            FRoleID = value;
        }
    }

    public string Notice
    {
        get
        {
            return FNotice;
        }
        set
        {
            FNotice = value;
        }
    }

    public string Inputer
    {
        get
        {
            return FInputer;
        }
        set
        {
            FInputer = value;
        }
    }

    public DateTime Input_Time
    {
        get
        {
            return FInput_Time;
        }
        set
        {
            FInput_Time = value;
        }
    }

    public DateTime Modify_Time
    {
        get
        {
            return FModify_Time;
        }
        set
        {
            FModify_Time = value;
        }
    }
}

/// <summary>
/// TEmployeeRS 的摘要说明。
/// </summary>
public class TEmployeeRS : TDataRset
{
    const string CTableName = "t_employee";
    const string CKeyName = "employee_id";

    public TEmployeeRS()
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
        FSqlOrderBy = "name asc";
        Load();
    }
}

/// <summary>
/// TEmployeeMenu 管理雇员权限
/// </summary>
public class TEmployeeMenu : TData
{
    //常量
    const string CTableName = "t_employee_menuitem";
    const string CKeyName = "id";

    //与数据表字段对应的变量
    private string FID;
    private string FEmployeeID;
    private string FMenuItemID;

    public TEmployeeMenu()
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
    }

    //插入新记录。
    public void Insert()
    {
        string sSql = "select count(*) from " + CTableName + " where menuitem_id='" + FMenuItemID + "' and employee_id='" + FEmployeeID + "'";
        int iID = TSqlDB.ExecuteScalar(sSql);
        if (iID == 0)
        {
            string ExSql = "newid(),'" + FEmployeeID + "','" + FMenuItemID + "'";
            base.Insert(ExSql);
        }
    }

    //根据TypeID获得记录值
    public override void Select()
    {
        string strSql = GetSelectString();
        TSqlDB oDB = new TSqlDB(strSql);
        if (!oDB.EOF)
        {
            FID = oDB["id"].ToString();
            FMenuItemID = oDB["menuitem_id"].ToString();
            FEmployeeID = oDB["employee_id"].ToString();
        }
        oDB.Free();
    }

    public static void DeleteItem(string EmployeeID, string MenuItemID)
    {
        string sSql = "delete " + CTableName + " where menuitem_id='" + MenuItemID + "' and employee_id='" + EmployeeID + "'";
        TSqlDB.ExecuteNonQuery(sSql);
    }

    public string MenuItemID
    {
        get
        {
            return FMenuItemID;
        }
        set
        {
            FMenuItemID = value;
        }
    }


    public string EmployeeID
    {
        get
        {
            return FEmployeeID;
        }
        set
        {
            FEmployeeID = value;
        }
    }
}
/// <summary>
/// TEmployeeColumn 管理栏目权限
/// </summary>
public class TEmployeeColumn : TData
{
    //常量
    const string CTableName = "t_employee_column";
    const string CKeyName = "id";

    //与数据表字段对应的变量
    private string FID;
    private string FEmployeeID;
    private string FColumnID;

    public TEmployeeColumn()
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
    }

    //插入新记录。
    public void Insert()
    {
        string sSql = "select count(*) from " + CTableName + " where column_id='" + FColumnID + "' and employee_id='" + FEmployeeID + "'";
        int iID = TSqlDB.ExecuteScalar(sSql);
        if (iID == 0)
        {
            string ExSql = "newid(),'" + FEmployeeID + "','" + FColumnID + "'";
            base.Insert(ExSql);
        }
    }

    //根据TypeID获得记录值
    public override void Select()
    {
        string strSql = GetSelectString();
        TSqlDB oDB = new TSqlDB(strSql);
        if (!oDB.EOF)
        {
            FID = oDB["id"].ToString();
            FColumnID = oDB["column_id"].ToString();
            FEmployeeID = oDB["employee_id"].ToString();
        }
        oDB.Free();
    }

    public static void DeleteItem(string EmployeeID, string ColumnID)
    {
        string sSql = "delete " + CTableName + " where column_id='" + ColumnID + "' and employee_id='" + EmployeeID + "'";
        TSqlDB.ExecuteNonQuery(sSql);
    }

    public string ColumnID
    {
        get
        {
            return FColumnID;
        }
        set
        {
            FColumnID = value;
        }
    }


    public string EmployeeID
    {
        get
        {
            return FEmployeeID;
        }
        set
        {
            FEmployeeID = value;
        }
    }
}
